package com.dyz.leetcode._08_StackAndQueue;

import java.util.ArrayDeque;

public class _946_validateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if(pushed.length!=popped.length){return false;}
        ArrayDeque<Integer> stack = new ArrayDeque();
        int i = 0;
        for(int num:pushed){
            stack.push(num);
            while(!stack.isEmpty() && stack.peek()==popped[i] && i<popped.length){
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }
}
